﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using MacomberMapSystem.Common.Database;
using System.Threading;
using System.Reflection;
using MacomberMapSystem.Common.Serialization;

namespace MacomberMapSystem.Common.User_Interfaces.Database
{
    /// <summary>
    /// This form displays the available models
    /// </summary>
    public partial class frmModels : DockContent
    {
        /// <summary>
        /// Initlalize the models form
        /// </summary>
        /// <param name="Models">The collection of models</param>
        public frmModels(Dictionary<int, MM_Database_Model> Models)
        {
            InitializeComponent();


            //Set up our column headings
            foreach (FieldInfo fI in typeof(MM_Database_Model).GetFields())
                if (MM_Serializable_Target.GetTargetType(fI) == MM_Serializable_Target.enumTargetType.FieldValue && fI.Name != "Name" && (fI.FieldType == typeof(int) || fI.FieldType == typeof(string) || fI.FieldType == typeof(MM_Database_Model) || fI.FieldType == typeof(MM_Database_User) || fI.FieldType == typeof(UInt64) || fI.FieldType == typeof(DateTime)))
                    lvModels.Columns.Add(fI.Name);

            SortedDictionary<int, MM_Database_Model> SortedModels = new SortedDictionary<int, MM_Database_Model>();
            foreach (MM_Database_Model Model in Models.Values)
                    SortedModels.Add(-Model.ID, Model);

            foreach (MM_Database_Model Model in SortedModels.Values)
            {
                ListViewItem lvI = lvModels.Items.Add(Model.Name);
                lvI.Tag = Model;
                foreach (FieldInfo fI in Model.GetType().GetFields())
                    if (fI.Name != "Name" && MM_Serializable_Target.GetTargetType(fI) == MM_Serializable_Target.enumTargetType.FieldValue)
                    {
                        Object inObj = fI.GetValue(Model);
                        if (inObj == null)
                            lvI.SubItems.Add("");
                        else if (inObj is DateTime)
                            if (((DateTime)inObj).ToOADate() == 0)
                                lvI.SubItems.Add("");
                            else
                                lvI.SubItems.Add(((DateTime)inObj).ToShortDateString());
                        else if (inObj is int)
                            lvI.SubItems.Add(((int)inObj).ToString("#,##0"));
                        else if (inObj is UInt64)
                            lvI.SubItems.Add(((UInt64)inObj).ToString("#,##0"));
                        else if (inObj is String || inObj is MM_Database_Model || inObj is MM_Database_User)                        
                            lvI.SubItems.Add(inObj.ToString());
                    }
            }

            //Resize all of our columns
            foreach (ColumnHeader col in lvModels.Columns)
            {
                lvModels.AutoResizeColumn(col.Index, ColumnHeaderAutoResizeStyle.HeaderSize);
                int MaxSize = col.Width;
                lvModels.AutoResizeColumn(col.Index, ColumnHeaderAutoResizeStyle.ColumnContent);
                if (col.Width < MaxSize)
                    col.Width = MaxSize;
            }   
        }

        /// <summary>
        /// When the user selects a model, update accordingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbModels_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvModels.SelectedItems.Count == 1)
            {
                MM_Database_Model Model = lvModels.SelectedItems[0].Tag as MM_Database_Model;
                if (Model.State == MM_Database_Model.enumModelState.Unopened)
                    if (MessageBox.Show("Open model " + Model.Name + "?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        return;
                    else
                        Model.InitiateLoading();
                        
                
                splMain.Panel2Collapsed = false;                
                ctlCIM.ClearElements();
                ctlCIM.SetConnector(Model);
            }
            else
                splMain.Panel2Collapsed = true;
        }
    }
}
